package com.sky.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.exception.OrderBusinessException;
import com.sky.properties.BaiduMapProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * 百度地图工具类
 */
@Slf4j
@Component
public class BaiduMapUtil {

    private static BaiduMapProperties baiduMapProperties;

    @Autowired
    private BaiduMapUtil(BaiduMapProperties baiduMapProperties){
        BaiduMapUtil.baiduMapProperties = baiduMapProperties;
    }

    //地理编码接口地址（商家）
    public static JSONObject getShopGeocoding() {
        String url = "https://api.map.baidu.com/geocoding/v3/?address="
                + baiduMapProperties.getAddress()
                + "&output=json&ak="
                + baiduMapProperties.getAk();

        String result = HttpClientUtil.doGet(url, null);

        JSONObject jsonResult = JSONObject.parseObject(result);

        jsonResult = jsonResult.getJSONObject("result");



        return jsonResult;
    }

    //地理编码地址（用户）
    public static JSONObject getUserGeocoding(String address) {
        String url = "https://api.map.baidu.com/geocoding/v3/?address="
                + address
                + "&output=json&ak="
                + baiduMapProperties.getAk();
        //+ "&callback=showLocation";这是一个用于跨域的回调函数，不需要

        String result = HttpClientUtil.doGet(url, null);

        JSONObject jsonResult = JSONObject.parseObject(result);

        jsonResult = jsonResult.getJSONObject("result");


        return jsonResult;
    }

    //骑行路径计算
    public static Integer getDistance(JSONObject shop, JSONObject user){
        String shopLat = shop.getJSONObject("location").getString("lat");//商家纬度值
        String shopLng = shop.getJSONObject("location").getString("lng");//商家经度值

        String userLat = user.getJSONObject("location").getString("lat");//用户纬度值
        String userLng = user.getJSONObject("location").getString("lng");//用户经度值

        //拼接url
        String url =
                "https://api.map.baidu.com/direction/v2/riding?origin="
                + shopLat + "," + shopLng
                +"&destination="
                + userLat + "," + userLng
                + "&ak="
                + baiduMapProperties.getAk();

        String s = HttpClientUtil.doGet(url, null);

        JSONObject jsonObject = JSONObject.parseObject(s);

//        jsonObject.getJSONObject("result")

        log.warn("异常内容{}", jsonObject);
        if (jsonObject.getString("status").equals("2002")){
            throw new OrderBusinessException(MessageConstant.OVER_DELIVERY_DISTANCE);
        }
        jsonObject = jsonObject.getJSONObject("result");

        JSONArray routes = jsonObject.getJSONArray("routes");

        JSONObject distanceObject = routes.getJSONObject(0);

        Integer distance = distanceObject.getInteger("distance");

        return distance;

    }
    //https://api.map.baidu.com/direction/v2/riding?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK

}

